package com.ruoyi.web.service.impl;

import java.util.List;
import java.util.Objects;

import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.web.mapper.DoctorDiseaseMapper;
import org.springframework.stereotype.Service;
import com.ruoyi.web.mapper.DoctorMapper;
import com.ruoyi.web.domain.Doctor;
import com.ruoyi.web.service.IDoctorService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 医生Service业务层处理
 * 
 * @author why
 * @date 2025-05-13
 */
@Service
public class DoctorServiceImpl implements IDoctorService 
{
    @Resource
    private DoctorMapper doctorMapper;

    @Resource
    private DoctorDiseaseMapper doctorDiseaseMapper;

    /**
     * 查询医生
     * 
     * @param id 医生主键
     * @return 医生
     */
    @Override
    public Doctor selectDoctorById(Long id)
    {
        return doctorMapper.selectDoctorById(id);
    }

    /**
     * 查询医生列表
     * 
     * @param doctor 医生
     * @return 医生
     */
    @Override
    public List<Doctor> selectDoctorList(Doctor doctor)
    {
        return doctorMapper.selectDoctorList(doctor);
    }

    /**
     * 新增医生
     * 
     * @param doctor 医生
     * @return 结果
     */
    @Override
    public int insertDoctor(Doctor doctor)
    {
        doctor.setCreateTime(DateUtils.getNowDate());
        doctor.setCreateBy(ShiroUtils.getSysUser().getLoginName());
        doctor.setCreateName(ShiroUtils.getSysUser().getUserName());

        doctor.setUpdateTime(DateUtils.getNowDate());
        doctor.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
        doctor.setUpdateName(ShiroUtils.getSysUser().getUserName());

        int i = doctorMapper.insertDoctor(doctor);

        if (!Objects.isNull(doctor.getDoctorDiseases())){
            doctor.getDoctorDiseases().forEach(bean -> {
                bean.setDoctorId(doctor.getId());
                bean.setCreateTime(DateUtils.getNowDate());
                bean.setCreateBy(ShiroUtils.getSysUser().getLoginName());
                bean.setCreateName(ShiroUtils.getSysUser().getUserName());

                bean.setUpdateTime(DateUtils.getNowDate());
                bean.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
                bean.setUpdateName(ShiroUtils.getSysUser().getUserName());

                doctorDiseaseMapper.insertDoctorDisease(bean);
            });
        }

        return i;
    }

    /**
     * 修改医生
     * 
     * @param doctor 医生
     * @return 结果
     */
    @Override
    public int updateDoctor(Doctor doctor)
    {
        doctor.setUpdateTime(DateUtils.getNowDate());
        doctor.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
        doctor.setUpdateName(ShiroUtils.getSysUser().getUserName());

        int i = doctorMapper.updateDoctor(doctor);

        if (!Objects.isNull(doctor.getDoctorDiseases())){
            doctor.getDoctorDiseases().forEach(bean ->{
                if (!Objects.isNull(bean.getId())){
                    bean.setUpdateTime(DateUtils.getNowDate());
                    bean.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
                    bean.setUpdateName(ShiroUtils.getSysUser().getUserName());
                    doctorDiseaseMapper.updateDoctorDisease(bean);
                }else{
                    bean.setDoctorId(doctor.getId());
                    bean.setCreateTime(DateUtils.getNowDate());
                    bean.setCreateBy(ShiroUtils.getSysUser().getLoginName());
                    bean.setCreateName(ShiroUtils.getSysUser().getUserName());

                    bean.setUpdateTime(DateUtils.getNowDate());
                    bean.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
                    bean.setUpdateName(ShiroUtils.getSysUser().getUserName());

                    doctorDiseaseMapper.insertDoctorDisease(bean);
                }
            });
        }

        return i;
    }

    /**
     * 批量删除医生
     * 
     * @param ids 需要删除的医生主键
     * @return 结果
     */
    @Override
    public int deleteDoctorByIds(String ids)
    {
        return doctorMapper.deleteDoctorByIds(Convert.toStrArray(ids),ShiroUtils.getSysUser().getLoginName());
    }

    /**
     * 删除医生信息
     * 
     * @param id 医生主键
     * @return 结果
     */
    @Override
    public int deleteDoctorById(Long id)
    {
        return doctorMapper.deleteDoctorById(id,ShiroUtils.getSysUser().getLoginName());
    }
}
